#include<cstdlib>
#include "swf.h"
#include "PlaceObject2.h"
#include "Matrix.h"

using namespace swfstream;
PlaceObject2* PlaceObject2::from_file(FILE* f) {
	PlaceObject2* r = new PlaceObject2;
	byte tmp = fgetc(f);
	r->PlaceFlagHasClipActions = ((tmp & 0x80) != 0);
	r->PlaceFlagHasClipDepth = ((tmp & 0x40) != 0);
	r->PlaceFlagHasName = ((tmp & 0x20) != 0);
	r->PlaceFlagHasRatio = ((tmp & 0x10) != 0);
	r->PlaceFlagHasColorTransform = ((tmp & 0x08) != 0);
	r->PlaceFlagHasMatrix = ((tmp & 0x04) != 0);
	r->PlaceFlagHasCharacter = ((tmp & 0x02) != 0);
	r->PlaceFlagMove = ((tmp & 0x01) != 0);
	fread(&r->depth, 2, 1, f);
	if (r->PlaceFlagHasCharacter) {
		fread(&r->characterId, 2, 1, f);
	}
	if (r->PlaceFlagHasMatrix) {
		r->matrix = Matrix::from_file(f);
	}
	if (r->PlaceFlagHasColorTransform) {
		fprintf(stderr, "not finished\n");
		exit(1);
	}
	if (r->PlaceFlagHasRatio) {
		fread(&r->ratio, 2, 1, f);
	}
	if (r->PlaceFlagHasName) {
		fprintf(stderr, "not finished\n");
		exit(1);
	}
	if (r->PlaceFlagHasClipDepth) {
		fread(&r->ClipDepth, 2, 1, f);
	}
	if (r->PlaceFlagHasClipActions) {
		fprintf(stderr, "not finished\n");
		exit(1);
	}
	return r;
}
void PlaceObject2::print() {
	Tag::print();
	printf("PlaceObject::\n");
	printf(
			"PlaceFlagHasClipActions:%u,PlaceFlagHasClipDepth:%u,PlaceFlagHasName:%u\nPlaceFlagHasRatio:%u,PlaceFlagHasColorTransform:%u,PlaceFlagHasMatrix:%u\nPlaceFlagHasCharacter:%u,PlaceFlagMove:%u\n",
			(uint) PlaceFlagHasClipActions, (uint) PlaceFlagHasClipDepth,
			(uint) PlaceFlagHasName, (uint) PlaceFlagHasRatio,
			(uint) PlaceFlagHasColorTransform, (uint) PlaceFlagHasMatrix,
			(uint) PlaceFlagHasCharacter, (uint) PlaceFlagMove);
	printf("depth:%u,", (uint) depth);
	if (PlaceFlagHasCharacter) {
		printf("characterId:%u,", (uint) characterId);
	}
	if (PlaceFlagHasMatrix) {
		matrix->print();
	}
	if (PlaceFlagHasRatio) {
		printf("ratio:%u,", (uint) ratio);
	}
	if (PlaceFlagHasClipDepth) {
		printf("ClipDepth:%u,", (uint) ClipDepth);
	}
	putchar('\n');
}

TiXmlElement* PlaceObject2::toTiXmlElement() {
	TiXmlElement* r = new TiXmlElement("_PlaceObject2");
	r->SetAttribute("PlaceFlagHasCharacter", this->PlaceFlagHasCharacter);
	r->SetAttribute("PlaceFlagHasClipActions", this->PlaceFlagHasClipActions);
	r->SetAttribute("PlaceFlagHasClipDepth", this->PlaceFlagHasClipDepth);
	r->SetAttribute("PlaceFlagHasColorTransform",
			this->PlaceFlagHasColorTransform);
	r->SetAttribute("PlaceFlagHasMatrix", this->PlaceFlagHasMatrix);
	r->SetAttribute("PlaceFlagHasName", this->PlaceFlagHasName);
	r->SetAttribute("PlaceFlagHasRatio", this->PlaceFlagHasRatio);
	r->SetAttribute("PlaceFlagHasFlagMove", this->PlaceFlagMove);
	r->SetAttribute("depth", this->depth);
	if (PlaceFlagHasCharacter) {
		r->SetAttribute("characterId", this->characterId);
	}
	if (PlaceFlagHasMatrix) {
		r->LinkEndChild(matrix->toTiXmlElement());
	}
	if (PlaceFlagHasColorTransform) {
		//r->SetAttribute(this->);
	}
	if (PlaceFlagHasRatio) {
		r->SetAttribute("ratio", this->ratio);
	}
	if (PlaceFlagHasName) {

	}
	if (PlaceFlagHasClipDepth) {

	}
	if (PlaceFlagHasClipActions) {

	}
	return r;
}
